var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    approvalList:[],
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '审批流程', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
  },
  // 加载审批流程
  loadApprovalList(){
    var _this = this;
    var defaultCompany = wx.getStorageSync('defaultCompany');
    if(defaultCompany == ''){
      defaultCompany = { id:0 };
    }
    requestClient.getApprovalProcessList(defaultCompany.id,function(res){
        _this.setData({
          approvalList:res.data.data
        });
    });
  },
  onShow(){
    var _this = this;
    _this.setData({
      approvalList:[]
    });
    requestClient.checkFunctionPermission(2001,'您没有权限设置审批流，请联系企业创建者！',function(){
      _this.loadApprovalList();
    },function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  },
  // 点击添加审批流
  bindAddApprovalProcessClick(e){
    var _this = this;
    requestClient.checkFunctionPermission(2001,'您没有权限添加审批流程，请联系企业创建者！',function(){
      var defaultCompany = wx.getStorageSync('defaultCompany');
      if(defaultCompany.authStatus != 1){
        if(_this.data.approvalList.length >= 1){
          wx.showModal({
            title:'未认证',
            content:'您的当前的企业没有完成认证，不能添加多个审批流！',
            confirmText:'去认证',
            success(r){
              if(r.confirm){
                wx.navigateTo({
                  url: '../../mine/companydetail/index?companyId=' + defaultCompany.id,
                })
              }
            }
          });
          return;
        }
      }
      wx.navigateTo({
        url: '../approvalprocess/index?processId=0&enableFlag=1',
      })
    },function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  },
  // 长按显示操作按钮
  bindLongPress(e){
    var _this = this;
    requestClient.checkFunctionPermission(2001,'您没有权限设置审批流，请联系企业创建者！',function(){
      var approval = e.currentTarget.dataset.approval;
      if(approval.enableFlag){
        wx.showActionSheet({
          itemList: ['禁用','删除'],
          itemColor:'#ff0000',
          success (res) {
            if(res.tapIndex == 0){
              approval.enableFlag = false;
              requestClient.updateApprovalProcess(approval,function(res){
                  _this.loadApprovalList();
              });
            } else {
              wx.showModal({
                title:'删除审批流',
                content:'您确定要删除此审批流吗？',
                success(r){
                  if(r.confirm){
                    requestClient.deleteApprovalProcess(approval.id,function(res){
                      _this.loadApprovalList();
                    })
                  }
                }
              })
            }
          }
        })
      } else {
        wx.showActionSheet({
          itemList: ['启用','删除'],
          success (res) {
            if(res.tapIndex == 0){
              approval.enableFlag = true;
              requestClient.updateApprovalProcess(approval,function(res){
                  _this.loadApprovalList();
              });
            } else {
              wx.showModal({
                title:'删除审批流',
                content:'您确定要删除此审批流吗？',
                success(r){
                  if(r.confirm){
                    requestClient.deleteApprovalProcess(approval.id,function(res){
                      _this.loadApprovalList();
                    })
                  }
                }
              })
            }
          }
        })
      }
    },function(){
      wx.navigateBack({
        delta: 1,
      })
    });
  }
})